﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Net;
using System.Text;
using System.Text.RegularExpressions;
using System.Windows.Forms;
using com.lover.common.http;
using com.lover.common;

namespace test
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                //string content = richTextBox1.Text;
                //int count = calcCnChars(content);
                //MessageBox.Show(String.Format("汉字个数:{0}", count));

                //string dt = DateTime.Now.ToString("R");
                //MessageBox.Show(dt);
                //string tester = "https://passport.6998.com/jump?clienttoken=j74E4rVDlYp8Ra1XtD%2fRsN1qae3uxUROx8DNig%2bE0Eq9GqWs5B%2bE3%2fmGUkNtJlbbyAkEgWie%2fET4%2bFJPjSP1AZmuzt5U6yoFcO18INuWLQD0ZjC9WKBXWVMpJijw3CsqMSpLx5ySX0k%3d&service=http://account.6998.com/mygame_login_astd-3-0.html";
                ////【典狱活动】11月20日0:00—11月22日24:00
                //Regex re = new Regex("https://passport\\.6998\\.com/jump\\?clienttoken=(.+)&service=http://account\\.6998\\.com/mygame_login_astd-\\d+-\\d+\\.html", RegexOptions.None);
                //MatchCollection matches = re.Matches(tester);
                //if (matches != null && matches.Count > 0 && matches[0] != null)
                //{
                //    if (matches[0].Groups != null && matches[0].Groups.Count >= 4)
                //    {
                //        string name = matches[0].Groups[1].Value;
                //    }
                //}

                string retstr = richTextBox1.Text;
                string restr = richTextBox2.Text;
                Regex re = new Regex(restr, RegexOptions.Multiline);
                Match m = re.Match(retstr);
                if (m != null && m.Groups != null && m.Groups.Count >= 2)
                {
                    string value = m.Groups[1].Value;
                    string v = value;
                    MessageBox.Show(v);
                }
                else
                {
                    MessageBox.Show("fail");
                }

                //string url = richTextBox2.Text;

                //HttpSocket sock = new HttpSocket();
                //sock.Url = url;
                //sock.Method = "GET";
                ////sock.Encoding = Encoding.GetEncoding("GBK");
                //HttpResult result = sock.execute();
                //if (result == null) return;
                //string msg = result.getContent();

                //HttpWebSocket sock = new HttpWebSocket();
                //string msg = sock.GetHtmlUseSocket(url);

                //HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
                //HttpWebResponse resp = (HttpWebResponse)req.GetResponse();
                //Stream stream = resp.GetResponseStream();
                //StreamReader sr = new StreamReader(stream, Encoding.UTF8);
                //string msg = sr.ReadToEnd();
                //sr.Close();

                //richTextBox1.Text = msg;

                //Image img = Image.FromStream(result.ContentStream);
                //pictureBox1.Image = img;

                //string raw = richTextBox1.Text;
                //byte[] bytes = Encoding.UTF8.GetBytes(raw);
                //int byte_length = bytes.Length;
                //byte[] final_bytes = new byte[byte_length + 15];
                //bytes.CopyTo(final_bytes, 0);
                //final_bytes[byte_length] = 0xa3;
                //final_bytes[byte_length + 1] = 0xac;
                //final_bytes[byte_length + 2] = 0xa1;
                //final_bytes[byte_length + 3] = 0xa3;
                //byte[] suffix_bytes = Encoding.UTF8.GetBytes("fdjf,jkgfkl");
                //suffix_bytes.CopyTo(final_bytes, byte_length + 4);
                //string enc = CommonUtils.generateBytesMd5(final_bytes);
                //richTextBox2.Text = enc.ToLower();
            }
            catch (System.Exception ex)
            {
                Console.WriteLine(String.Format("ERROR::{0}::{1}", ex.Message, ex.StackTrace));
            }
        }
        private int calcCnChars(string content)
        {
            int count = 0;
            for (int i = 0, len = content.Length; i < len; i++)
            {
                string tmp = content.Substring(i, 1);
                byte[] bytes = Encoding.UTF8.GetBytes(tmp);
                byte b = bytes[0];
                if (b - 160 > 15) count++;
            }
            return count;
        }
    }
}
